package com.doyens.common.config.utils;



import com.doyens.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {

    public static List<SysMenu> buildTree(List<SysMenu> allSysMenuList) {

        ArrayList<SysMenu> treeMenuList = new ArrayList<>();

        for (SysMenu sysMenu : allSysMenuList) {
            if(sysMenu.getParentId().longValue() == 0){
                treeMenuList.add(getChildren(sysMenu,allSysMenuList));
            }
        }
        return treeMenuList;
    }

    // 递归
    private static SysMenu getChildren(SysMenu sysMenu, List<SysMenu> allSysMenuList) {
        sysMenu.setChildren(new ArrayList<>());
        for (SysMenu menu : allSysMenuList) {
            if(menu.getParentId().longValue() == sysMenu.getId()){
                sysMenu.getChildren().add(getChildren(menu,allSysMenuList));
            }
        }
        return sysMenu;
    }
}
